#include "ZoomEffect.h"
#include "TimeController.h"
#include "Utils.h"
#include "Vector.h"

ZoomEffect::ZoomEffect(float xCenter, float yCenter, float zoomFrom, float zoomTo,
					   float alphaFrom, float alphaTo, float timeLife, int numFrame, int width, int height)
					   :Object(0, 0, width, height, 0)
{
	center = new Vector(xCenter, yCenter);

	this->numFrame = numFrame;
	this->timeDt = timeLife/numFrame;
	
	this->zoomCur = this->zoomFrom = zoomFrom;
	this->zoomTo = zoomTo;
	this->zoomDt = (zoomTo-zoomFrom)/numFrame;

	this->alphaCur = this->alphaFrom = alphaFrom;
	this->alphaTo = alphaTo;
	this->alphaDt = (alphaTo-alphaFrom)/numFrame;
	isAlive = false;
	nextFrameDelay = 0;
	pos->Set(center->x - width*zoomCur/2, center->y - height*zoomCur/2);
}

void ZoomEffect::Update()
{
	float t = TimeController::GetInstance()->GetDtInSecond();

	nextFrameDelay += t;
	if(nextFrameDelay>timeDt)
	{
		nextFrameDelay-=timeDt;
		zoomCur += zoomDt;
		alphaCur += alphaDt;
	}

	pos->Set(center->x - width*zoomCur/2, center->y - height*zoomCur/2);

	if((zoomDt>0&&zoomCur>zoomTo)||(zoomDt<0&&zoomCur<zoomTo))
		isAlive =false;
	else isAlive = true;
}

void ZoomEffect::Restart()
{
	zoomCur = zoomFrom;
	alphaCur = alphaFrom;
	isAlive = true;
}

ZoomEffect::~ZoomEffect()
{
	DELETE_SAFELY(center);
}